package com.leyou.trade.web;

import com.leyou.trade.entity.CartItem;
import com.leyou.trade.service.CartItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 付
 */
@RestController
@RequestMapping("cart")
public class Cartontroller {
    @Autowired
    private CartItemService cartItemService;
    //新增商品到购物车
    @PostMapping
    public ResponseEntity<Void> saveCartItem(@RequestBody CartItem cartItem){
    cartItemService.saveCartItem(cartItem);
    return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //查询购物车
    @GetMapping("list")
    public ResponseEntity<List<CartItem>> queryCartList() {
        return ResponseEntity.ok(cartItemService.queryCartList());
    }
    //更新购物车中商品的数量

    @PutMapping
    public ResponseEntity<Void> updateCartNum(@RequestParam("id") Long skuid,
                                              @RequestParam("num") Integer num){

    cartItemService.updateCartNum(skuid,num);
    return ResponseEntity.noContent().build();
    }

    //删除
    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId") Long skuId) {
        cartItemService.deleteCart(skuId);
        return ResponseEntity.noContent().build();
    }

    //登录后购物车合并
    @PostMapping("list")
    public ResponseEntity<Void> savasAll(@RequestBody List<CartItem> itemList){
        cartItemService.savasAll(itemList);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
